<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>嵌套对象查询 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-query.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/nested-query.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="nested-objects.html">嵌套对象</a></span>
»
<span class="breadcrumb-node">嵌套对象查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="nested-mapping.html">« 嵌套对象映射</a>
</span>
<span class="next">
<a href="nested-sorting.html">使用嵌套字段排序 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="nested-query"></a>嵌套对象查询<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/402_Nested/32_Nested_query.asciidoc">edit</a>
</h2>
</div></div></div>
<p>由于嵌套对象被索引在单独的隐藏的文档中，无法直接查询。相应地，必须使用 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-nested-query.html" class="ulink" target="_top"> <code class="literal">nested</code> 查询</a> 去获取：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /my_index/blogpost/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "eggs" <a id="CO271-1"></a><i class="conum" data-value="1"></i>
          }
        },
        {
          "nested": {
            "path": "comments", <a id="CO271-2"></a><i class="conum" data-value="2"></i>
            "query": {
              "bool": {
                "must": [ <a id="CO271-3"></a><i class="conum" data-value="3"></i>
                  {
                    "match": {
                      "comments.name": "john"
                    }
                  },
                  {
                    "match": {
                      "comments.age": 28
                    }
                  }
                ]
              }
            }
          }
        }
      ]
}}}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO271-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">title</code> 子句是查询根文档的。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO271-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">nested</code> 子句作用于嵌套字段 <code class="word">comments</code> 。在此查询中，既不能查询根文档字段，也不能查询其他嵌套文档。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO271-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">comments.name</code> 和 <code class="word">comments.age</code> 子句操作同一个嵌套文档。</p>
</td>
</tr>
</table>
</div>
<div class="tip admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">nested</code> 字段可以包含其他的 <code class="literal">nested</code> 字段。同样地，<code class="literal">nested</code> 查询也可以包含其他的 <code class="literal">nested</code> 查询。而嵌套的层次会按照你所期待的被应用。</p>
</div>
</div>
<p>当然，一个<code class="literal">nested</code>查询可以匹配多个嵌套文档。每一个匹配的嵌套文档都有自己的相关度得分，但是这众多的分数最终需要汇聚为可供根文档使用的一个分数。</p>

<p>默认情况下，根文档的分数是这些嵌套文档分数的平均值。可以通过设置 <code class="literal">score_mode</code> 参数来控制这个得分策略，相关策略有 <code class="literal">avg</code> (平均值), <code class="literal">max</code> (最大值), <code class="literal">sum</code> (求和) 和 <code class="literal">none</code> (直接返回固定分数值<code class="literal">1.0</code>)。</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /my_index/blogpost/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "eggs"
          }
        },
        {
          "nested": {
            "path": "comments",
            "score_mode": "max", <a id="CO272-1"></a><i class="conum" data-value="1"></i>
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "comments.name": "john"
                    }
                  },
                  {
                    "match": {
                      "comments.age": 28
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO272-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>返回最佳匹配的嵌套文档的<code class="literal">_score</code>给根文档。</p>
</td>
</tr>
</table>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>如果 <code class="literal">nested</code> 查询放在一个布尔查询的 <code class="literal">filter</code> 子句中，其表现就像一个 <code class="literal">nested</code> 查询，只是 <code class="literal">score_mode</code> 参数不再生效。因为它被用于不打分的查询中 — 只是符合或不符合条件，不打分 — 那么 <code class="literal">score_mode</code> 就没有任何意义，因为根本就没有要打分的地方。</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="nested-mapping.html">« 嵌套对象映射</a>
</span>
<span class="next">
<a href="nested-sorting.html">使用嵌套字段排序 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>